package org.ourgrid.peer.business.requester;
import java.util.ArrayList;
import java.util.List;

import org.ourgrid.common.internal.IResponseTO;
import org.ourgrid.common.internal.RequesterIF;
import org.ourgrid.common.internal.response.ReleaseResponseTO;
import org.ourgrid.peer.business.dao.PeerDAOFactory;
import org.ourgrid.peer.request.RemoteWorkerProviderIsDownRequestTO;
import org.ourgrid.peer.to.AllocableWorker;


public class RemoteWorkerProviderIsDownRequester implements RequesterIF<RemoteWorkerProviderIsDownRequestTO> {


	public List<IResponseTO> execute(RemoteWorkerProviderIsDownRequestTO request) {
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
	
		String providerAddress = request.getProviderAddress();
		
		List<AllocableWorker> allAllocableWorkers = PeerDAOFactory.getInstance().getAllocationDAO().getAllAllocableWorkers();
		for (AllocableWorker allocable : allAllocableWorkers) {
			if (allocable.getProviderAddress().equals(providerAddress)) {
				allocable.setProviderAddress(null);
			}
		}
		
		ReleaseResponseTO releaseTO = new ReleaseResponseTO();
		releaseTO.setStubAddress(providerAddress);
		
		responses.add(releaseTO);
		
		return responses;
	}

}
